//
//  ConsultationCollectionView.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/1/31.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit

private let itemSize: CGFloat = 199
var recommendModels :[SearchRecommendModel] = []

class RecommendCollectionView: UICollectionView, UICollectionViewDelegate, UICollectionViewDataSource {
    let banner = ["subBanner1.jpg", "subBanner2", "subBanner3.jpg", "subBanner4.jpg", "subBanner5.jpg"]
    init() {
        super.init(frame: .zero, collectionViewLayout: UICollectionViewLayout())
        setUI() 
    }
    
    var delegateVc: UIViewController?
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func awakeFromNib() {
        setUI()
    }
    
    func setUI() {
        self.showsHorizontalScrollIndicator = false
        self.delegate = self
        self.dataSource = self
        self.register(UINib(nibName: "\(ConsultationCollectionViewCell.self)", bundle: nil), forCellWithReuseIdentifier: "cell")
        self.backgroundColor = ONColor.cellBGColor
        
        let followLayout = UICollectionViewFlowLayout()
        followLayout.minimumInteritemSpacing = 20
        followLayout.itemSize = CGSize(width: itemSize, height: self.height - 10)
        followLayout.sectionInset = UIEdgeInsetsMake(0, 10, 10, 10)
        followLayout.scrollDirection = .horizontal
        self.contentOffset = .zero
        self.collectionViewLayout = followLayout
    }
    
    func setData(arr:[SearchRecommendModel]) {
        
        recommendModels.removeAll()
        recommendModels.append(contentsOf: arr)
        self.layoutIfNeeded()
        
        self.reloadData()
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return recommendModels.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? ConsultationCollectionViewCell
        let model = recommendModels[indexPath.row]
        if let image = model.image{
            let path:String = "\(BaseURL)\(image)"
            let url = NSURL.init(string: path)
            cell?.showImageView.sd_setImage(with: url! as URL, completed: nil)
        }
        cell?.titleLabel.text = model.title!
        cell?.typeLabel.setTitle("为您推荐", for: UIControlState.normal)
        return cell!
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let model1 = recommendModels[indexPath.row]
        var model: BannerModel = BannerModel()
        model.id = model1.id
        model.type = model1.type
        model.url = model1.url
        model.coverPath = model1.image
        self.delegateVc?.bannerJump(model: model)
    }
}
